<template>
  <div class="article">
    <el-card>
      <template #header>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <el-form label-width="100px">
        <el-form-item label="状态">
          <!-- 单选框组件 shift + ctrl + alt + 下 -->
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 频道 -->
        <el-form-item label="频道">
          <!-- <el-select placeholder="请选择" v-model="form.channel_id">
            <el-option
              v-for="item in channelsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select> -->
          <HmSelect :form="form"></HmSelect>
        </el-form-item>
        <!-- 日期 -->
        <el-form-item label="日期">
          <el-date-picker
            v-model="form.picker"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submit">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <template #header> 根据筛选条件共查询到{{ total }}条数据 </template>
      <el-table :data="list">
        <el-table-column label="封面">
          <template v-slot="scope">
            <img style="width: 100px" :src="scope.row.cover.images[0]" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            {{ scope.row.status == 2 ? "审核通过" : "审核不通过" }}
          </template>
        </el-table-column>
        <el-table-column
          label="发布时间"
          width="320"
          prop="pubdate"
        ></el-table-column>
        <el-table-column label="阅读数" prop="read_count"></el-table-column>
        <el-table-column label="评论数" prop="comment_count"></el-table-column>
        <el-table-column label="点赞数" prop="like_count"></el-table-column>
        <el-table-column label="操作" width="300">
          <template v-slot="scope">
            <div>
              <el-button :index="scope.$index" @click="edit(scope.row)"
                >编辑</el-button
              >
              <el-button @click="del(scope)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="sizeChange"
        @current-change="pageChange"
        :current-page="form.page"
        :page-sizes="[1, 10, 20, 30, 40]"
        :page-size="form.per_page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { channels, mpArticles, delArticles } from "@/api/index.js";
export default {
  data() {
    return {
      form: {
        picker: [],
        status: null,
        channel_id: "",
        begin_pubdate: "",
        end_pubdate: "",
        page: 1,
        per_page: 10
      },
      channelsList: [],
      list: [],
      total: 0
    };
  },
  watch: {
    "form.picker": {
      handler(newVal, oldVal) {
        if (newVal === 2) {
          this.form.begin_pubdate = newVal[0];
          this.form.end_pubdate = newVal[1];
        }
      },
      deep: true
    }
  },
  async created() {
    const res = await channels();
    this.channelsList = res.data.channels;
    console.log(res.data.channels);
    // 进入页面自动获取文章列表
    this.getDate();
  },
  methods: {
    async getDate() {
      // 这个方法用于获取文章列表
      const res = await mpArticles(this.form);
      this.list = res.data.results;
      this.total = res.data.total_count;
    },
    async submit() {
      // 点击筛选后，获取文章列表
      this.form.page = 1;
      this.getDate();
    },
    sizeChange(size) {
      this.form.per_page = size;
      this.form.page = 1;
      this.getDate();
    },
    pageChange(page) {
      this.form.page = page;
      this.getDate();
    },
    del(scope) {
      // 文章ID
      const articleId = scope.row.id;

      this.$confirm("您确定删除这条数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          console.log("确定");
          // 1. 跟后端请求，删除某篇文章
          await delArticles(articleId);
          this.$message.success("删除成功");

          this.submit();
        })
        .catch(() => {
          console.log("取消");
        });
    },
    edit(row) {
      // 点编辑按钮后，带上当前要编辑的文章ID跳转到发布页面
      this.$router.push("/publish?id=" + row.id);
    }
  }
};
</script>
<style></style>
